package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.commonEvent.ColorPreviewAdminEvent;
	import com.tongji.housediy.events.commonEvent.RotationRelatedEvent;
	import com.tongji.housediy.events.commonEvent.SetCompStateEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.SelectedObjectModel;
	import com.tongji.housediy.utils.ConstVariableInventory;
	import com.tongji.housediy.view.components.RoomBorder;
	
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.RadioButton;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class WallColorWindowMediator extends Mediator
	{
		[Inject]
		public var view:WallColorWindowTitle;
		
		[Inject]
		public var model:GoodsModel;
		
		[Inject]
		public var selectedModel:SelectedObjectModel;
		
		override public function onRegister():void
		{
			ConstVariableInventory.isShowColorPicker = true;
			
			(view.wallRadioBtn.skin as FloorRadioBtn).image.source = new Bitmap(model.wallMap);
			(view.skirtingRadioBtn.skin as FloorRadioBtn).image.source = new Bitmap(model.skirtingMap);
			
			mediatorMap.createMediator(view.wallMaterialView);
			mediatorMap.createMediator(view.wallMaterialView.popupBtn.popUp);
			
			view.okBtn.addEventListener(MouseEvent.CLICK, okBtnClickHandler);
			view.cancelBtn.addEventListener(MouseEvent.CLICK, closeBtnClickHandler);
			view.closeBtn.addEventListener(MouseEvent.CLICK, closeBtnClickHandler);
			view.wallRadioBtn.addEventListener(MouseEvent.CLICK, wallRadioBtnClickHandler);
			view.skirtingRadioBtn.addEventListener(MouseEvent.CLICK, skirtingRadioBtnClickHandler);
			
			view.addEventListener(ColorPreviewAdminEvent.CHANGE_IMAGE, changeImageColor);
		}
		
		private function changeImageColor(event:ColorPreviewAdminEvent):void
		{
			((view.WallColorGroup.selection as RadioButton).skin as FloorRadioBtn).image.source = new Bitmap(event.bitmapData);
		}
		
		private function wallRadioBtnClickHandler(event:MouseEvent):void
		{
			//view.viewStack.selectedIndex = 0;
			model.arrangeSizes(view.wallMaterialView.tempWallGoods, "墙面色彩");
			mediatorMap.createMediator(view.wallMaterialView);
			mediatorMap.createMediator(view.wallMaterialView.popupBtn.popUp);
			
			dispatch(new SetCompStateEvent(SetCompStateEvent.RESET_WALL_LIST_INDEX));
		}
		
		private function skirtingRadioBtnClickHandler(event:MouseEvent):void
		{
			//view.viewStack.selectedIndex = 1;
			
			model.arrangeSizes(view.wallMaterialView.tempSkirtingGoods, "踢脚线色彩");
			/*if (!mediatorMap.hasMediatorForView(view.skirtingMaterialView))
			{
				view.skirtingMaterialView.tempSkirtingGoods = (selectedModel.selectedObj as RoomBorder).skirtingHostGoods;
				model.arrangeSizes(view.skirtingMaterialView.tempSkirtingGoods, "踢脚线色彩");
				
				mediatorMap.createMediator(view.skirtingMaterialView);
				mediatorMap.createMediator(view.skirtingMaterialView.popupBtn.popUp);
			}*/
			
			dispatch(new SetCompStateEvent(SetCompStateEvent.RESET_SKIRTING_LIST_INDEX));
		}
		
		private function closeBtnClickHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view.wallMaterialView.popupBtn.popUp);
			mediatorMap.removeMediatorByView(view.wallMaterialView);
			if (mediatorMap.hasMediatorForView(view.wallMaterialView))
			{
				mediatorMap.removeMediatorByView(view.wallMaterialView.popupBtn.popUp);
				mediatorMap.removeMediatorByView(view.wallMaterialView);
			}
			mediatorMap.removeMediatorByView(view);
		}
		
		private function okBtnClickHandler(event:MouseEvent):void
		{
			if (view.wallMaterialView.tempWallPicModel || view.wallMaterialView.tempWallMap)
				(selectedModel.selectedObj as RoomBorder).changeSizeAndColor(view.wallMaterialView.tempWallPicModel, view.wallMaterialView.tempWallMap, view.wallMaterialView.tempWallGoods);
			if (view.wallMaterialView.tempSkirtingPicModel || view.wallMaterialView.tempSkirtingMap)
				(selectedModel.selectedObj as RoomBorder).changeSkirtingSizeAndColor(view.wallMaterialView.tempSkirtingPicModel, view.wallMaterialView.tempSkirtingMap, view.wallMaterialView.tempSkirtingGoods);
			
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view.wallMaterialView.popupBtn.popUp);
			mediatorMap.removeMediatorByView(view.wallMaterialView);
			if (mediatorMap.hasMediatorForView(view.wallMaterialView))
			{
				mediatorMap.removeMediatorByView(view.wallMaterialView.popupBtn.popUp);
				mediatorMap.removeMediatorByView(view.wallMaterialView);
			}
			mediatorMap.removeMediatorByView(view);
			
			dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
			dispatch(new RotationRelatedEvent(RotationRelatedEvent.HIDE_ROTATION_BTN));
		}
	}
}